Strict

include "../common/simplex_font.bmx"
include "glowing_sokurah_line.bmx"

'd3d9max2ddriver()
SetGraphicsDriver GLMax2DDriver()
Graphics 800,600
Global xres# = GraphicsWidth()
Global yres# = GraphicsHeight()

Global Image:TImage = LoadImage("dot.png")'makeImg()'LoadImage("clock.png") ' 32x32 256x256 e.t.c
'Global TestImage:TImage = makeImg2()'LoadImage("sun.png")

Global tx# = 10
Global ty# = 150

Global numframes:Int = 0

While Not KeyHit(KEY_ESCAPE)
	' Note to self: OpenGL draws front face normally Counter Clockwise. CCW.
	'SetBlend AlphaBlend
	SetBlend LightBlend
	
	SetScale 1,1
	SetAlpha 1.0
	SetColor 255,255,0
	LineCool(0,0,800,600,35)
	
	SetAlpha 1.0
	SetColor 255,255,255
	LineCool(0,600,800,0,25)
	
	SetAlpha 1.0
	SetColor 0,255,0
	LineCool(400,300,MouseX(),MouseY(),55)
	
	Local maxchars:Int = simplex.length
	tx = xres/2
	ty = 180
	drawChar( Max(0, Min(MouseX()/xres*maxchars,simplex.length+1) ) ,5)
	
	tx = 20
	ty = 70
	SetAlpha 0.5
	drawstring("Kim must goto .bed",2.5)
	
	tx = 100
	ty = 400
	SetAlpha 1.0
	SetColor 0,32,128
	'SetBlend AlphaBlend
	Local t# = Cos( MilliSecs() / 1000.0 * 360 )
	If t < 0
	drawstring("vector Text  \o/",1)   tx = 100 ty = 450
	drawstring("              |",1)	 	tx = 100 ty = 500
	drawstring("             /\",1)	    ' tx = 100 ty = 500
	Else
	drawstring("vector Text  o",1)   tx = 100 ty = 450
	drawstring("            /|\ ",1)	 	tx = 100 ty = 500
	drawstring("            /\",1)	    ' tx = 100 ty = 500
	EndIf	
	
	'glBlendFunc GL_ONE, GL_ONE
	glBlendFunc GL_SRC_ALPHA,GL_ONE
      'setblend lightblend
	SetColor 128,32,64
	lissajous(xres/2,yres/2)
	'SetBlend(LIGHTBLEND)
	'glEnable GL_BLEND
	'glBlendFunc GL_SRC_ALPHA,GL_ONE
	'glDisable GL_ALPHA_TEST 'Important! GL turns this on for text methinks. 
	
	Flip
	Cls
	numframes:+1
Wend

Function lissajous(tx#,ty#)
	Local time# = MilliSecs() / 1000.0
	Local angofs# = time
	Local a# = 3 + Sin(time/50)
	Local b# = 2 + Cos(time/50)
	Local segs# = 200
	Local r# = 100
	Local ox# = Sin(a*angofs+180)*r
	Local oy# = Sin(b*angofs)*r
	'rotate2d(ox,oy,MouseX() / xres*360)
	ox:+tx
	oy:+ty
	For Local t=0 To segs
		Local ang# = t*360/(segs)
		ang = ang + angofs
		Local x# = Sin(a*ang+180)*r
		Local y# = Sin(b*ang)*r
		
		'rotate2d(x,y,MouseX() / xres*360)
		x:+tx
		y:+ty
		
		LineCool( ox,oy,x,y, 30 )
		ox = x
		oy = y
	Next
End Function

Function rotate2d(x# Var , y# Var, angle#)
   Local x1# = x;
   Local y1# = y;
   Local sa# = Sin(angle);
   Local ca# = Cos(angle);
   
   x = x1 * ca - y1 * sa;
   y = y1 * ca + x1 * sa;
End Function

Function  drawstring(str$, textsiz#)
	For Local i:Int=0 Until str.length
		Local c:Int = str[i] - 32;
		drawChar(c, textsiz);
		tx :+ simplex[c][1]*textsiz;
	Next
End Function

Function drawChar(c:Int,scale#)
   Local verts:Int = simplex[c][0];
   Local spacing# = simplex[c][1];
   
	Local ax:Int = simplex[c][2];
	Local ay:Int = simplex[c][3]; 
	Local ox# = tx+ax*scale-scale*2
	Local oy# = ty-ay*scale+scale*2
   'moveto(tx+ax*scale-scale*2, ty-ay*scale+scale*2); 
	Local i:Int = 0 
   For i=4 Until (verts*2) Step 2
      ax = simplex[c][i+0];
      ay = simplex[c][i+1];

      If(ax=-1 Or ay = -1)
		ox = tx+simplex[c][i+2]*scale-scale*2
		oy = ty-simplex[c][i+3]*scale+scale*2
	'moveto(tx+simplex[c][i+2]*scale-scale*2, ty-simplex[c][i+3]*scale+scale*2);
      Else 
		LineCool(ox,oy,tx+ax*scale-scale*2, ty-ay*scale+scale*2,35)
		ox = tx+ax*scale-scale*2
		oy = ty-ay*scale+scale*2
		'lineto(tx+ax*scale-scale*2, ty-ay*scale+scale*2); 
	EndIf       
   Next
	LineCool(ox, oy, tx+simplex[c][i+0]*scale-scale*2, ty-simplex[c][i+1]*scale+scale*2,35)
   'lineto(tx+simplex[c][i+0]*scale-scale*2,ty-simplex[c][i+1]*scale+scale*2);
End Function 


Function makeImg:TImage ()
	Local img:TImage = CreateImage(64,64)
	Local pixmap:TPixmap = LockImage(img)
	drawCircleGradient(pixmap)
	UnlockImage(img)
	Return img
End Function

Function makeImg2:TImage ()
	Local img:TImage = CreateImage(128,128)
	Local px:TPixmap = LockImage(img)
	
	Local siz:Float = px.width
	Local center:Float = (siz-1) / 2.0
   	For Local y:Int=0 Until siz
   	For Local x:Int=0 Until siz
     	 	Local t:Float = Sqr( (center-x)*(center-x) + (center-y)*(center-y) ) / center
		'Local c:Int = 255-(t)*255
		Local c:Int = 250-(t^1.0)*255
		If( c < 0 ) c = 0
		If( c > 255 ) c = 255
		
		Local argb:Int = ( c Shl 24) + 255 Shl 16 + c Shl 8 + c
		WritePixel px,x,y,argb      	
   	Next
	Next 
	
	UnlockImage(img)
	Return img
End Function

Function drawCircleGradient(px:TPixmap)
	Local siz:Float = px.width
	Local center:Float = (siz-1) / 2.0
   	For Local y:Int=0 Until siz
   	For Local x:Int=0 Until siz
     	 	Local t:Float = Sqr( (center-x)*(center-x) + (center-y)*(center-y) ) / center
		
		Local a:Int = 250-(t^0.20)*255
		If( a < 0 ) a = 0
		If( a > 255 ) a = 255
		
		Local c:Int = 255-(t)*255
		If( c < 0 ) c = 0
		If( c > 255 ) c = 255
		
		Local argb:Int = ( a Shl 24) + c Shl 16 + c Shl 8 + c
		WritePixel px,x,y,argb      	
   	Next
	Next 
End Function
